package org.fuxian.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.fuxian.common.Constraints;
import org.fuxian.domain.MySort;
import org.fuxian.service.IMySortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/sort")
public class MySortController {


    @Autowired
    IMySortService sortService;

    @RequestMapping("/findAllSort")
    @ResponseBody
    public Object findAllSort(int current) {
        PageHelper.startPage(current, Constraints.PAGE_SIZE);
        List<MySort> list = sortService.findAll();
        return new PageInfo(list);
    }

    @RequestMapping("/findAllName")
    @ResponseBody
    public Object findAllName() {
        List<MySort> list = sortService.findAll();
        return list;
    }



    @RequestMapping("/toUpdate/{sid}")
    public String toUpdate(@PathVariable("sid") Integer sid, Model model) {
        MySort mysort = sortService.findById(sid);
        model.addAttribute("sort", mysort);
        return "sort/upp";
    }

    @RequestMapping("/findSortByName")
    public @ResponseBody
    String findSortByNameOrId(Integer sid, String sname, Model model) {
        MySort mysort = sortService.findSortByName(sname);
        int count = 0;
        if (mysort != null)
            count = 1;
        return count + "";
    }

    @RequestMapping("/update")
    @ResponseBody
    public String update(Integer sid, String sname, Model model) {
        int count = sortService.update(sname, sid);
        return count + "";
    }


    @RequestMapping("/del")
    @ResponseBody
    public String del(Integer sid) {
        int count = sortService.delete(sid);
        return count + "";
    }

    @RequestMapping("/addSort")
    @ResponseBody
    public String addSort(String sname) {
        int count = sortService.add(sname);
        return count + "";
    }

}
